package edu.whu.notepal.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 发送邮件工具类 MailUtil
 *
 * @author 陈磊
 * @date 2021/3/22 16:52
 */

@Component
public class EmailUtil{
    private static final String SUBJECT = "注册验证码";

    private static String CONTENT_PREFIX = "【NOTEPAL】:验证码:";

    private static String CONTENT_SUFFIX = "。有效期"+ RedisConstants.LOGIN_CODE_TTL+"分钟，请勿泄露。";

    @Autowired
    private JavaMailSender mailSender;

    // 配置文件中我的qq邮箱
    @Value("${spring.mail.from}")
    private String from;

    /**
     * 简单文本邮件
     * @param to 收件人
     * @param code 验证码
     */
    public void sendSimpleMail(String to, String code) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(SUBJECT);
        //邮件内容
        message.setText(CONTENT_PREFIX+code+CONTENT_SUFFIX);
        //发送邮件
        mailSender.send(message);
    }

}